package jp.reflexworks.invoice.model;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Invoice {
 
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	// アプリによる楽観的ロック用
	public long revision;

	public void setRevision(long revision) {
		this.revision = revision;
	}

	@Persistent
	// 1だとtrue(削除済)
	public int deleted;

	@Persistent
	private InvoiceBase invoiceBase; 
	
	@Persistent
	public long id;
	
	public String title;
	
	@Persistent
	public String invoiceNo;
	@Persistent
	public String issuedDate;
	@Persistent
	public String myCompanyName;
	@Persistent
	public String zip;
	@Persistent
	public String address;
	@Persistent
	public String phone;
	@Persistent
	public String companyName;
	public String summary;
	public String invoiceTitle;

	@Persistent
	public String total;
	@Persistent
	public String job;
	
	public String seqno;
	public String description;
	public String quantity;
	public String unitPrice;
	public String lineTotal;
	
	@NotPersistent               // Persistent(mappedBy = "invoice")  
	public List<Order> order;		// 明細レコードは請求レコードの子供
	
	public String subtotal;
	
	@Persistent
	public String subtotal1;
	
	public String salestax;
	@Persistent
	public String salestax1;		// 請求書レコードでも集計項目を格納する

	public String totalTd;
	@Persistent
	public String totalTd1;			// 請求書レコードでも集計項目を格納する

	public String note;
	@Persistent
	public String note1;
	public String note2;
	public String note3;
	
	public Invoice() {
		this.setup();
	}

	/*
	 * mura 2009-08-04
	 * falseならsetup()を実行させない
	 * 
	 */
	public Invoice(boolean b) {
		if (b) {
			this.setup();
		}
	}	
	
	public Invoice(long id) {
		this.id = id;
		this.setId(id);
		this.setup();
	}
	
    public void setId(long id) {
        this.id = id;
    }

    
    
	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		
		System.out.println("setKey(Key key): " + key);
		
		this.key = key;
	}

	private void setup() {
		// 静的表示項目（本来はEntityを別途定義すべき）
		this.title = "御請求書";
		this.myCompanyName = "有限会社バーチャルテクノロジー";
		this.zip = "105-0055";
		this.address = "東京都港区芝1-2-3";
		this.phone = "031232222";
		this.summary = "下記の通り御請求申し上げます。";
		this.invoiceTitle = "御請求金額";
		this.seqno = "No";
		this.description = "品名";
		this.quantity = "数量";
		this.unitPrice = "単価（円）";
		this.lineTotal = "金額（円）";
		this.subtotal = "合計";
		this.salestax = "消費税";
		this.totalTd = "御請求金額";
		this.note = "備考";
		this.note2 = "②　お振り込みの際は、下記口座にお願い致します。手数料は貴社にてご負担願います。";
		this.note3 = "③　振込先：バーチャル銀行芝町支店普通預金２０１２３４５〔口座名　有限会社バーチャルテクノロジー〕";

	}
	
	// View -> JDO 
	public void convertTo() {
		// カンマを外す
		this.total = this.total.replaceAll(",",""); 	
	}

	// JDO -> View 
	public void convertFrom() {
		
		// 明細の合計を計算
		int sub = 0;
		for(Order aRecord:order) {
			if (aRecord.lineTotal!=null&&!aRecord.lineTotal.equals("")) {
				sub += Integer.parseInt(aRecord.lineTotal);
				aRecord.convertFrom();
			}
		}
		//　消費税計算
		int tax = (sub * 5)/100;
		
		// カンマをつける
		DecimalFormat formatter = new DecimalFormat("#,###"); 		 
		this.subtotal1 = formatter.format(sub);
		this.salestax1 = formatter.format(tax);
		this.total = formatter.format(sub + tax); 	
		this.totalTd1 = this.total; 	
	}
	
	public void addOrder(Order _order) {
		if (order == null) {
			setOrder(new ArrayList<Order>());  
		}
		
		List<Order> listOrder = getOrder();
		listOrder.add(_order); 
		setOrder(listOrder);
	}

	// Requestオブジェクトから詰めるためのもの
	public void setId(String id) {
		this.id = Long.parseLong(id);
	}

	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getInvoiceNo() {
		return invoiceNo;
	}
	public void setInvoiceNo(String invoiceNo) {
		this.invoiceNo = invoiceNo;
	}
	public String getIssuedDate() {
		return issuedDate;
	}
	public void setIssuedDate(String issuedDate) {
		this.issuedDate = issuedDate;
	}
	public String getMyCompanyName() {
		return myCompanyName;
	}
	public void setMyCompanyName(String myCompanyName) {
		this.myCompanyName = myCompanyName;
	}
	public String getZip() {
		return zip;
	}
	public void setZip(String zip) {
		this.zip = zip;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getCompanyName() {
		return companyName;
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	public String getSummary() {
		return summary;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public String getInvoiceTitle() {
		return invoiceTitle;
	}
	public void setInvoiceTitle(String invoiceTitle) {
		this.invoiceTitle = invoiceTitle;
	}
	public String getTotal() {
		return total;
	}
	public void setTotal(String total) {
		this.total = total;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getSeqno() {
		return seqno;
	}
	public void setSeqno(String seqno) {
		this.seqno = seqno;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getQuantity() {
		return quantity;
	}
	public void setQuantity(String quantity) {
		this.quantity = quantity;
	}
	public String getUnitPrice() {
		return unitPrice;
	}
	public void setUnitPrice(String unitPrice) {
		this.unitPrice = unitPrice;
	}
	public String getLineTotal() {
		return lineTotal;
	}
	public void setLineTotal(String lineTotal) {
		this.lineTotal = lineTotal;
	}
	public List<Order> getOrder() {
		return order;
	}
	public void setOrder(List<Order> listOrder) {
		this.order = listOrder;
	}
	public String getSubtotal() {
		return subtotal;
	}
	public void setSubtotal(String subtotal) {
		this.subtotal = subtotal;
	}
	public String getSubtotal1() {
		return subtotal1;
	}
	public void setSubtotal1(String subtotal1) {
		this.subtotal1 = subtotal1;
	}
	public String getSalestax() {
		return salestax;
	}
	public void setSalestax(String salestax) {
		this.salestax = salestax;
	}
	public String getSalestax1() {
		return salestax1;
	}
	public void setSalestax1(String salestax1) {
		this.salestax1 = salestax1;
	}
	public String getTotalTd() {
		return totalTd;
	}
	public void setTotalTd(String totalTd) {
		this.totalTd = totalTd;
	}
	public String getTotalTd1() {
		return totalTd1;
	}
	public void setTotalTd1(String totalTd1) {
		this.totalTd1 = totalTd1;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public String getNote1() {
		return note1;
	}
	public void setNote1(String note1) {
		this.note1 = note1;
	}
	public String getNote2() {
		return note2;
	}
	public void setNote2(String note2) {
		this.note2 = note2;
	}
	public String getNote3() {
		return note3;
	}
	public void setNote3(String note3) {
		this.note3 = note3;
	}


	public long getId() {
		return id;
	}

	public int getDeleted() {
		return deleted;
	}

	public void setDeleted(int deleted) {
		this.deleted = deleted;
	}

	public long getRevision() {
		return revision;
	}
	
	// EntityConvertoer用
	public void setDeleted(Long deleted) {
		this.deleted = (int)((long) deleted);
	}



//	//clone
//	public Object clone() throws CloneNotSupportedException {
//		
//		Invoice cloned = (Invoice) super.clone();
//		
//		cloned.setAddress(address);
//		cloned.setCompanyName(companyName);
//		cloned.setDeleted(deleted);
//		cloned.setDescription(description);
//		cloned.setId(id);
//		cloned.setInvoiceNo(invoiceNo);
//		cloned.setInvoiceTitle(invoiceTitle);
//		cloned.setIssuedDate(issuedDate);
//		cloned.setJob(job);
//		//cloned.setKey(key);
//		cloned.setLineTotal(lineTotal);
//		cloned.setMyCompanyName(myCompanyName);
//		cloned.setNote(note);
//		cloned.setNote1(note1);
//		cloned.setNote2(note2);
//		cloned.setNote3(note3);
//		//cloned.setOrderList(record);
//		cloned.setOrderList((new ArrayList<Order>()));
//		cloned.setPhone(phone);
//		cloned.setQuantity(quantity);
//		cloned.setRevision(revision);
//		cloned.setSalestax(salestax);
//		cloned.setSalestax1(salestax1);
//		cloned.setSeqno(seqno);
//		cloned.setSubtotal(subtotal);
//		cloned.setSubtotal1(subtotal1);
//		cloned.setSummary(summary);
//		cloned.setTitle(invoiceTitle);
//		cloned.setTotal(lineTotal);
//		cloned.setTotalTd(totalTd);
//		cloned.setTotalTd1(totalTd1);
//		cloned.setUnitPrice(unitPrice);
//		cloned.setZip(zip);
//		
//		return cloned;
//	}
	
	
	
}